<?php
namespace App\Service\Member;

class AddressService extends BaseService
{
    //用户收货地址

    /**
     * 列表
     * @param int $uid
     * @param array $pages【start，limit】
     */
    public function getList($uid, $pages)
    {
        $start = ($pages['start'] - 1) * $pages['limit'];
        $db = $this->dbConn($this->dbUaddress)
            ->where('del', 0)
            ->where('uid', $uid);
        $total = $db->count();
        $res = $db->skip($start)->take($pages['limit'])
            ->orderBy('id', 'asc')
            ->get();
        if (!count($res)) {
            return $this->toback();
        }
        $arys = [];
        foreach ($res as $v) {
            $v = (array) $v;
            $arys[] = [
                'id' => $v['id'],
                'accepter' => $v['accepter'],
                'phone' => $v['phone'],
                'map_name' => $v['map_name'],
                'map_locate' => $v['map_locate'],
            ];
        }
        return $this->toback(0, '', $arys, $total);
    }

    /**
     * 详情
     * @param int $id
     */
    public function getOne($id)
    {
        $res = (array) $this->dbConn($this->dbUaddress)
            ->where('del', 0)
            ->where('id', $id)
            ->first();
        if (empty($res)) {
            return $this->toback(-2, '没有记录');
        }
        $arys = [
            'id' => $res['id'],
            'accepter' => $res['accepter'],
            'phone' => $res['phone'],
            'map_name' => $res['map_name'],
            'map_locate' => $res['map_locate'],
            'door' => $res['door'],
        ];
        return $this->toback(0, '', $arys);
    }

    /**
     * 添加
     * @param int $uid 用户UID
     * @param array $pas 请求参数
     */
    public function add($pas)
    {
        $pas['addtime'] = $pas['uptime'] = toDate();
        $lastid = $this->dbConn($this->dbUaddress)->insertGetId($pas);
        if (!$lastid) {
            return $this->toback(-1, 'error');
        }
        return $this->toback();
    }

    /**
     * 修改
     * @param int $id 地址ID
     * @param array $pas 请求参数
     */
    public function modify($id, $pas)
    {
        $res = (array) $this->dbConn($this->dbUaddress)
            ->where('del', 0)
            ->where('id', $id)
            ->first();
        if (empty($res)) {
            return $this->toback(-2, '地址不存在');
        }
        $pas['uptime'] = toDate();
        $up = $this->dbConn($this->dbUaddress)->where('id', $id)->update($pas);
        if (!$up){
            return $this->toback(-1, 'error');
        }
        return $this->toback();
    }

    /**
     * 修改
     * @param int $id 地址ID
     */
    public function del($id)
    {
        $res = (array) $this->dbConn($this->dbUaddress)
            ->where('del', 0)
            ->where('id', $id)
            ->first();
        if (empty($res)) {
            return $this->toback(-2, '地址不存在');
        }
        $arys = [
            'del' => 1,
            'deltime' => toDate(),
        ];
        $up = $this->dbConn($this->dbUaddress)->where('id', $id)->update($arys);
        if (!$up){
            return $this->dbConn(-1, 'error');
        }
        return $this->toback();
    }
}